package company.tree;



public class TrieTree {
    TrieNode root;

    public static void main(String[] args) {
        TrieTree trieTree = new TrieTree();
        trieTree.insertTrieTree("hello");
        trieTree.insertTrieTree("world");
        trieTree.insertTrieTree("java");
        trieTree.insertTrieTree("program");
        trieTree.insertTrieTree("art");
        System.out.println(trieTree.search("python"));
        System.out.println(trieTree.search("java"));
    }

    public TrieTree() {
        root = new TrieNode();
    }

    public void insertTrieTree(String word) {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++) {
            int idx = word.charAt(i) - 'a';
            if (node.arr[idx] == null) {
                node.arr[idx] = new TrieNode();
            }
            node = node.arr[idx];
        }
        node.isEnd = true;
    }

    public boolean startSwith(String prefix) {
        TrieNode node = root;
        for (int i = 0; i < prefix.length(); i++) {
            int x = prefix.charAt(i) - 'a';
            if (node.arr[x] == null) {
                return false;
            }
            node = node.arr[x];
        }
        return true;
    }

    public boolean search(String word) {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++) {
            int x = word.charAt(i) - 'a';
            if (node.arr[x] == null) {
                return false;
            }
            node = node.arr[x];
        }
        return node.isEnd;
    }
}
